<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>과목생성</title>
</head>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript">

 $(document).ready(function() {

	$('#output').empty();
	
	fn_searchList();
	
	$('#update').on('click', function(){
		fn_updateGwamok();
    });
	
	$('#insert').on('click', function(){
	
		fn_dupValidate();
		
    });
  
	var strValue = "";
	$('#delete').on('click', function(){
		$("input:checked[name='chk']:checked").each(function(){
			if(this.checked){
				strValue += this.value;
				
			}
		});
		
		var form = document.form;
		form.strdelete.value = strValue; 
		form.action = "gwamokDeleteMng.do";
		form.submit();
		
	 });
	
	$('#search').on('click', function(){
		fn_searchList();
    });
	

});


</script>
<script>

    function fn_searchList(){

    	$('#output').empty();
    	

  		 for(var i=0 ; i<form.jg.length; i++){
			if(form.jg[i].checked && form.jg[i].value != ''){
	
				form.junkong_gubun.value = form.jg[i].value; 
	
			};
		}
    	
    	var frm = jQuery("form[name=form]");
    	var dataString = frm.serialize();
    
    	$.ajax({ 
              type : "GET",
              url:"gwamokAjaxMng.do",
              dataType:"json",
         	  data :  dataString,
              success : function(data) {
            	  var output = '';
    		  			output += '<table>';
    		  			output += '<tr id="thfix">                 ';
    		      		output += '	<th> ';
    		      		output += '		선택              ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		과목코드          ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		과목명            ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		전공과            ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		전공여부          ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		학점          ';
    		      		output += '	</th>               ';
    		      		output += '</tr>                ';

    		      	  for(var i=0; i<data.result.length; i++) {
    		      	      var obj = data.result[i];


    						output += '<tr>';
    						output += '<td><input type=\"checkbox\" id=\"chk\" name=\"chk\" value=' + obj.code +'/></td>';
    						output += '<td><a href=\"#\" name=\"detail\" id=\"detail\" onclick=\"fn_detailGwamok(\''+obj.code+'\');\">' + obj.code + '</a></td>';
    						output += '<td>' + obj.name + '</td>';
    						output += '<td>' + obj.gwa_code + '</td>';
    						output += '<td>' + obj.junkong_gubun + '</td>';
    						output += '<td>' + obj.hakjum + '</td>';
    						output += '</tr>';
    						
    		      	  }
    		  	  output += '</table>';
    		  	  $('#output').append(output);
    		  	 

              },
              complete : function(data) {
              },
              error : function(xhr, status, error) {
                    alert("에러발생" + status + ','+ error + ','+ xhr);
              }
        });
    	
    }

    function fn_dupValidate(){    
    	$.ajax({ 
            type : "GET",
            url:"gwamokdupMng.do?code=" + document.form.code.value ,
            success : function(data) {
            	if(data == 'Y'){
            		document.form.code.value = "";
            		document.form.code.focus();
            		alert("과목이 중복되었습니다.");
            		return;
            	}else{
            		fn_insertGwamok();
            	}
            },
            complete : function(data) {
            },
            error : function(xhr, status, error) {
                  alert("에러발생" + status + ','+ error + ','+ xhr);
            }
      });	
    }
    
	function numValue(obj){
	   var t = escape(obj);
	   if(t.match(/^\d+$/ig) == null){
	     return false;
	   }else{
	     return true;
	   }
	   
	   return true;
	}

	function fn_validate(){
		var form = document.form;
		
		if(form.code.value == ''){
			alert("과목 코드를 입력해 주세요");
			form.code.focus();
			return false;
		}
		
		if(form.name.value == ''){
			alert("과목 이름을 입력해 주세요");
			form.name.focus();
			return false;
		}
		
		if(form.hakjum.value == ''){
			alert("학점을 입력해 주세요");
			form.hakjum.focus();
			return false;
		}
		
		if(!numValue(form.hakjum.value)){
			alert("학점은 숫자만 넣어 주세요");
			form.hakjum.focus();
			return false;
		}
     
		var cnt =0;
		for(var i=0 ; i<form.jg.length; i++){
			if(form.jg[i].value == ''){
				alert("전공 여부를 선택해 주세요");
				return false;
			}
			if(!form.jg[i].checked){
								
				cnt++;
			}
		}	
		
		 if(cnt == 3){
			alert("전공여부를 선택해 주세요");
			return false;
		} 

		if(form.day1.value ==''){
			alert("수업 요일을 선택해 주세요");
			form.day1.focus();
			return false;
		}
		
		if(form.time1_1.value  == '' && form.time1_2.value  == '' && form.time1_3.value  == '' && form.time1_4.value  == ''){
			alert("수업 시간을 선택해 주세요");
			form.time1_1.focus();
			return false;
		}
		for(var i = 1 ; i<5 ; i++){
			for(var j=i+1 ; j<5; j++){
				var a =eval('form.time1_' + i + '.value');
				var b =eval('form.time1_' + j + '.value');
				
				if(a !='' & b !=''){
					if(a == b ){
						alert("수업시간이 중복되었습니다.다른 시간을 선택해주세요");
						return;
					}
				}
			}
		}
		if(form.day1.value == form.day2.value){
			alert("같은 요일을 선택할 수 없습니다.");
			return false;
		}
		
		return true;
	}
	
	function fn_insertGwamok(){

		var form =  document.form;

		if(fn_validate()){
			
			for(var i=0 ; i<form.jg.length; i++){
				if(form.jg[i].checked){

					form.junkong_gubun.value = form.jg[i].value; 
				};
			}	
			form.action = "gwamokInsertMng.do";
			form.submit();
		};
	}
	
	function fn_delGwamok(){

			var form =document.form;
			var cnt = 0;
			var strDel = "";
			
			for(var i =0 ; i<form.chk.length; i++)
			{
			  if(form.chk[i].checked){
				  strDel = strDel + form.chk[i].value + '/';  
				  cnt++;
			  };
			}
		
			if(cnt == 0 ){
				alert("삭제 할 항목을 선택해 주세요");
				return;
			}
			
			form.strdelete.value = strDel; 
			form.action = "gwamokDeleteMng.do";
			form.submit();
	}
	function fn_updateGwamok(){
		
		var form = document.form;
		
		for(var i=0 ; i<form.jg.length; i++){
			if(form.jg[i].checked ==''){
				form.junkong_gubun.value = form.jg[i].value; 	
			}			
		}	
		form.strcode.value = form.code.value ;
		form.action = "gwamokUpdateMng.do";
		form.submit();		
	}

	function fn_detailGwamok(obj){

		var form = document.form;
		form.strcode.value = obj; 
		form.action = "gwamokDetailMng.do";
		form.submit();
	}
	
	function fn_listGwamok(){

		var form = document.form;
		form.action = "gwamokMng.do";
		form.submit();
	}

	function fn_back(){
		alert("리스트화면으로 이동합니다.");
		var form = document.form;
		form.action = "gwamokMng.do";
		form.submit();
	}
	function init(){
		var form = document.form;
		var hcode =form.hidencode.value;
		
		if(hcode != '' && form.code.value != ''){
			
			var el = document.getElementById("code");
			el.disabled = true;
			
		}
	}
	
</script>
<body onload="init();">
<form name="form" method="post" >
	<input type="hidden" name ="junkong_gubun" value="">
	<input type="hidden" name ="strdelete" id="strdelete" value="">
	<input type="hidden" name ="strcode" value="">
	<input type="hidden" name ="hidencode" value="${vo.code}">
<table class="table1" width="800" >
	<tr>
		<td>
			<table class="table2">
				<tr>
					<th>
						과목코드
					</th>
					<td>
						<INPUT TYPE="text" NAME="code" id="code" value='${vo.code}' maxlength=5 >
					</td>
					<th>
						과목명
					</th>
					<td>
						<INPUT TYPE="text" NAME="name" value='${vo.name}'>
					</td>
			    </tr>
			    <tr>
					<th>
						학점
					</th>
					<td>
						<INPUT TYPE="text" NAME="hakjum" value='${vo.hakjum}'>
					</td>
							
					<th>
						전공여부
					</th>
					<td>
						<input type="radio" name="jg" value="1" <c:if test="${vo.junkong_gubun == '1'}"> checked </c:if>/> 전공필수
						<input type="radio" name="jg" value="2" <c:if test="${vo.junkong_gubun == '2'}"> checked </c:if>/> 전공선택
						<input type="radio" name="jg" value="3" <c:if test="${vo.junkong_gubun == '3'}"> checked </c:if>/> 교양
					</td>
			    </tr>
			   	<tr>
					<th>
						전공과
					</th>
					<td>				
 						${gwaName }
 						<input type="hidden" name="gwa_code" value=${gwa_code } />
 					</td>
			    </tr>
			     <tr>
					<th>
						요일 선택 1
					</th>
					<td>
						<select name="day1">
							<option value="">선택</option>
							<option value="1" <c:if test="${vo.day1 == '1'}"> selected </c:if> >월</option>
							<option value="2" <c:if test="${vo.day1 == '2'}"> selected </c:if> >화</option>
							<option value="3" <c:if test="${vo.day1 == '3'}"> selected </c:if> >수</option>
							<option value="4" <c:if test="${vo.day1 == '4'}"> selected </c:if> >목</option>
							<option value="5" <c:if test="${vo.day1 == '5'}"> selected </c:if> >금</option>
						</select>
					</td>
					<th>
						시간 선택 1
					</th>
					<td >
						<select name="time1_1" >
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time1_1 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time1_2" onchange="fn_change('time1_2','1','2')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time1_2 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time1_3" onchange="fn_change('time1_3','1','3')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time1_3 == i}">  selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time1_4" onchange="fn_change('time1_4','1','4')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'   <c:if test="${vo.time1_4 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>					
						</td>					
			    </tr>
			     <tr>
					<th>
						요일 선택 2
					</th>
					<td>
						<select name="day2">
							<option value="">선택</option>
							<option value="1" <c:if test="${vo.day2 == '1'}"> selected </c:if>>월</option>
							<option value="2" <c:if test="${vo.day2 == '2'}"> selected </c:if>>화</option>
							<option value="3" <c:if test="${vo.day2 == '3'}"> selected </c:if>>수</option>
							<option value="4" <c:if test="${vo.day2 == '4'}"> selected </c:if>>목</option>
							<option value="5" <c:if test="${vo.day2 == '5'}"> selected </c:if>>금</option>
						</select>
					</td>
					<th>
						시간 선택 2
					</th>
					<td>
						<select name="time2_1" >
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time2_1 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time2_2" onchange="fn_change('time2_2','2','2')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time2_2 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time2_3" onchange="fn_change('time2_3','2','3')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'  <c:if test="${vo.time2_3 == i}">  selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						
						<select name="time2_4" onchange="fn_change('time2_4','2','4')">
						<option value="">선택</option>
						<c:forEach var="i" begin="1" end="10">
							<option value='${i}'   <c:if test="${vo.time2_4 == i}"> selected </c:if> >${i} 교시</option>
						</c:forEach>
						</select>
						</td>
				</tr>
			    <tr>
					<td colspan= 4>
						<input type="button" id="insert" value="신규저장"  /> 														
						<input type="button" id="delete" value="삭제"  /> 
						<input type="button" id="search" value="조회"  />
						<input type="button" id="update" value="수정"  />
						<input type="button" id="exit" value="리스트보기" onclick="fn_back()" />
					</td>
				</tr>
			</table>
			<div id="output" class="defTable"></div> 
			</td>
			</tr>
</table>

 </form>
</body>

</html>
 